from django.contrib.auth import authenticate
from rest_framework import serializers

import users
from cart.models import Cartproduct
from products.models import Products
from users.models import Users

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = Users
        fields = '__all__'
# accounts/serializers.py

from rest_framework import serializers
from django.contrib.auth import authenticate

class UserRegistrationSerializer(serializers.ModelSerializer):
    class Meta:
        model = Users
        fields = ['username', 'password']
        extra_kwargs = {'password': {'write_only': True}}

    def create(self, validated_data):
        user = Users.objects.create_user(
            username=validated_data['username'],
            password=validated_data['password'],
        )
        return user

class UserLoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, data):
        username = data.get('username')
        password = data.get('password').ljust(20)
        user = authenticate(username=username, password=password)
        if user:
            return user
        raise serializers.ValidationError("Incorrect Credentials")
class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        #model是Meta类中的一个属性，指定了这个序列化器是基于哪个模型生成的。
        fields = '__all__'
        #fields 是 Meta 类中的另一个属性，用于指定序列化器应该包含哪些模型字段。
        #是一个特殊的值，表示包含模型中的所有字段。这意味着序列化器将自动包含 Product 模型中的所有字段，
class CartproductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Cartproduct
        fields = '__all__'